//
//  LoginCiewController.swift
//  hunan_ios_ldnsm
//
//  Created by I-Q-I on 2019/4/28.
//  Copyright © 2019 I-Q-I. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
class LoginViewController: UBaseViewController {
    
    var isEye = false
    
    var loginViewModel = LoginModel()
    
    
    var loginView = LoginView()

    override func configNavigationBar() {
        super.configNavigationBar()
        navigationItem.title = "登录"
        navigationController?.navigationBar.isTranslucent = false
    }
    override func configUI() {
        
        loginView = LoginView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        view.addSubview(loginView)
        loginView.loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
         loginView.registerBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        
    }
    @objc func register(){
        self.navigationController?.pushViewController(RegisterViewController(), animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }




    @objc func login(){
        IQKeyboardManager.shared.resignFirstResponder()
        let username = loginView.phoneInput.text!.trim
        let password = loginView.pwdInput.text!.trim

        loginViewModel.login(by: ["username": username, "password": password], completion: {
            (result, message, data) in
            if result {
                let user = data as! User
                 UserDefaults.standard.setUser(userId: user.user_id, roleType: user.roleType, authStatus: user.authStatus, token: user.token)
                self.present(UTabBarController(), animated: false, completion: {})
            } else {
                Toast.toastError(message)
            }
        })
    }
    

    
}
